<!DOCTYPE html>
<html>
<head>
    <title>Game Eat Fish</title>
    <script type="text/javascript" src="js/ivank.js"></script>
    <script type="text/javascript" src="js/t.js"></script>    
</head>

<body><canvas id="c" width="1366" height="633"></canvas>
    <script type="text/javascript">
        var m, stage;
        function webGLStart() {
            //code
            stage = new Stage("c");
            stage.addEventListener(Event.RESIZE, resizeGame);
            
            m = new EFMain(stage.stageWidth, stage.stageHeight);
            stage.addChild(m);
            m.resize(stage.stageWidth, stage.stageHeight);
        }
        
        function resizeGame(e) {
            //code
            m.resize(stage.stageWidth, stage.stageHeight);
        }
        
        function EFMain(width, height) {
            //code
            Resizable.call(this);
            this.w = width;
            this.h = height;
            
            this.game = new Controller(width, height);
            this.game.addEventListener2("over", this.over, this);
            this.addChild(this.game);
            
            this.panel = new Panel();
            this.panel.pbtn.addEventListener2(MouseEvent.CLICK, this.goPlay, this);
            this.addChild(this.panel);
            this.panel.y = height*1.5;
            this.panel.Show((height - 300)/2);
            
            this.auth = new CText("Love Give. Copyright", 5);
            this.auth.x = 20;
            this.addChild(this.auth);
        }
        
        EFMain.prototype = new Resizable();
        
        EFMain.prototype.resize = function(wi, hi){
            Resizable.prototype.resize.call(this, wi, hi);
            
            this.game.resize(wi, hi);
            this.auth.y = hi - 20 - this.auth.GetHeight();
            
            this.panel.x = (wi - 541) * 0.5;
            if (!this.panel.hidden) { 
                //code
                this.panel.y = (hi - this.panel.GetHeight()) * 0.5;                
            }else{
                this.panel.y = hi + 200;
            }
        }
        
        EFMain.prototype.over = function(e){
            this.panel.Show((this.h - 300)/2);
        }
        
        EFMain.prototype.goPlay = function(e){
            this.panel.Hide(this.h + 100);
            this.game.Start();
        }
        function Panel() {
            //code.
            Sprite.apply(this);
            
            this.hidden = false;
            
            var title = new Bitmap(new BitmapData("images/title.png"));
            this.addChild(title);
            
            var man1 = new CText("Eat fish that are smaller than you.", 3);
            var man2 = new CText("Avoid fish that are bigger than you.", 3);
            
            man1.x = 60; man2.x = 55;
            man1.y = 135; man2.y = 180;
            
            this.addChild(man1);
            this.addChild(man2);
            
            var vers = new CText("CopyRight", 2, 18);
            vers.x = 435; vers.y = 94;
            this.addChild(vers);
            
            this.pbtn = new Button("Play");
            this.pbtn.x = 230;
            this.pbtn.y = 250;
            this.addChild(this.pbtn);
        }
        
        Panel.prototype = new Sprite();
        Panel.prototype.Show = function(ny){
            this.hidden = false;
            Tweener.addTween(this, {y:ny, time:0.5});
        }
        
        Panel.prototype.Hide = function(ny){
            this.hidden = true;
            Tweener.addTween(this, {y:ny, time : 0.5});
        }
        
        function Fish() {
            //code
            Sprite.apply(this);
            
            this.mouseChildren = false;
            this.mouseEnabled = false;
            
            this.t = 0;
            this.v = 0;
            this.rad = 100;
            
            if (Fish.bodyTex == null) {
                //code
                Fish.bodyTex = new BitmapData("images/body.png");
                Fish.eyesTex = new BitmapData("images/eye.png");
                Fish.flipTex = new BitmapData("images/flip.png");
            }
            
            this.body = new Bitmap(Fish.bodyTex);
            this.body.x = -180;
            this.body.y = -140;
            this.addChild(this.body);
            
            this.flip = new Bitmap(Fish.flipTex);
            this.flip.x = 20;
            this.flip.y = 45;
            this.addChild(this.flip);
            
            this.leye = new MBitmap(Fish.eyesTex, 5);
            this.reye = new MBitmap(Fish.eyesTex, 5);
            
            this.leye.x = 55;  this.leye.y = -75;
            this.leye.scaleX = this.leye.scaleY = 0.8;
            this.reye.x = 10; this.reye.y = -65;
            
            this.addChild(this.leye);
            this.addChild(this.reye);
            
            this.blinking = false;
            this.blinkI = 0;            
        }
        
        Fish.prototype = new Sprite();
        
        Fish.prototype.update = function(){
            ++this.t;
            this.flip.rotation = 60 + Math.sin(this.v * this.t/this.rad) * 40;
            if (this.blinking) {
                //code
                var n = (this.blinkI < 5) ? this.blinkI : (9 - this.blinkI);
                this.leye.gotoAndStop(n);
                this.reye.gotoAndStop(n);
                ++this.blinkI;
                if (this.blinkI == 10) {
                    //code
                    this.blinking = false;
                    this.blinkI = 0;
                }
            }else {
                this.blinking = Math.random() < 0.02;
            }
            
        }
        
        Fish.prototype.UpdateRadDir = function(rad, dirx){
            if (rad == this.rad) {
                //code
                return;
            }
            this.v = 40 * dirx / rad;
            this.rad = rad;
            var nsc = 0.01 * this.rad;
            Tweener.addTween(this, {scaleX:((dirx < 0)?-1:1)*nsc, scaleY:nsc, transition:"easeOutElastic", time: 0.5});
        }
        function Button(text) {
            //code
            Sprite.apply(this);
            
            this.mouseChildren = false;
            this.buttonMode = true;
            this.tf = new TextField();
            this.tf.setTextFormat(Button.tfr);
            this.tf.text = text;
            this.tf.width = this.tf.textWidth;
            this.tf.height = this.tf.textHeight;
            this.tf.x = 8;
            this.tf.y = 4;
            this.addChild(this.tf);
            
            this.graphics.beginFill(0xffff7700);
            this.graphics.drawRect(0, 0, this.tf.GetWidth() + 16, this.tf.GetHeight() + 8);
        }
        
        Button.prototype  = new Sprite();
        
        Button.tfr = new TextFormat("Trebuchet MS", 36, 0xffffff, true);
        
        function CText(t, p, s) {
            //code
            Sprite.apply(this);
            
            this.mouseChildren = false;
            if (!p) { 
                //code
                p = 2;
            }
            if (!s) {
                //code
                s = 26;
            }
            
            this.tfr = new TextFormat("Trebuchet MS", s, 0xffffff);
            
            this.tf = new TextField();
            this.tf.setTextFormat(this.tfr);
            this.tf.text = t;
            this.tf.width = this.tf.textWidth;
            this.tf.height = this.tf.textHeight;
            this.tf.x = this.tf.y = p;
            this.addChild(this.tf);
            this.pad = p;
            
            this.graphics.beginFill(0x000000, 0.4);
            this.graphics.drawRect(0, 0, this.tf.GetWidth() + 2 * p, this.tf.GetHeight() + 2 * p);
        }
        CText.prototype = new Sprite();
        
        CText.prototype.SetText = function(t){
            this.tf.setText(t);
        }
        
        function Controller(width, height) {
            //code
            Resizable.apply(this);
            this.w = width;
            this.h = height;
            this.MARGIN = 20;
            this.pause = true;
            
            this.bg = new Background(width, height);
            this.addChild(this.bg);
            
            this.model = new Model();
            this.view = new View(this.model);
            this.addChild(this.view);
            
            this.topLength = 10;
            if (readCookie("topLength") != null) {
                //code
                this.topLength = Math.floor(readCookie("topLength"));
            }
            
            this.cln = new FlipNum(30, 0xffffff, "", " cm");
            this.addChild(this.cln);
            this.tln = new FlipNum(20, 0xffffff, "Top length: ", " cm");
            this.addChild(this.tln);
            this.tln.SetDirectly(this.topLength);
            
            this.addEventListener2(Event.ENTER_FRAME, this.onEF, this);
        }
        Controller.prototype = new Resizable();
        
        Controller.prototype.Start = function(){
            this.model.Reset();
            this.cln.SetDirectly(this.model.mfish.rad);
            this.UpdateViewScale();
            this.pause = false;;
        }
        
        Controller.prototype.resize = function(w, h){
            Resizable.prototype.resize.call(this, w, h);
            this.bg.resize(w, h);
            this.model.SetSizeRatio(w/h);
            this.view.x = 0.5 * w;
            this.view.y = 0.5 * h;
            this.tln.x  = this.tln.y = this.MARGIN;
            this.cln.x = this.MARGIN;
            this.cln.y = this.MARGIN + 28;
            this.UpdateViewScale();
        }
        
        Controller.prototype.UpdateViewScale = function(){
            if (this.model.mapry == 0) {
                //code
                return;
            }
            var wi = this.w; var hi = this.h;
            var sc = 0.5 * hi/this.model.mapry;
            Tweener.addTween(this.view, {scaleX:sc, scaleY:sc, transition:"easeInOutSine", time:1});
            this.bg.UpdateScale(sc);
        }
        
        Controller.prototype.onEF = function(e){
            if (this.paused) {
                //code
                return;
            }
            this.model.SetFishPos(this.view.mouseX, this.view.mouseY);
            this.model.Step();
            this.view.Update();
            
            var crad = this.model.mfish.rad;
            this.cln.SetNum(crad);
            
            if (crad > this.topLength) {
                //code
                this.topLength = crad;
                this.tln.SetNum(crad);
                createCookie("topLength", crad, 360);
            }
            
            if (this.model.IsOver()) {
                //code
                this.paused = true;
                this.dispatchEvent(new Event("over"));
            }
            
            if(this.model.SizeChanged()) this.UpdateViewScale();
        }
        function FlipNum(s, c, pf, sf) {// size , color, prefix, xsuffix
            //code
            Sprite.apply(this);
            
            if (s == null) {
                //code
                s = 26;
            }
            if (c== null) {
                //code
                c = 0xffffff;                
            }
            if (pf == null) {
                //code
                pf = "";
            }
            if (sf==null) {
                //code
                sf = "";
            }
            
            this.prefix = pf;
            this.suffix = sf;
            this.cnum = -1;// current number;
            this.tnum = -1;// target number;
            
            this.tfr = new TextFormat("Trebuchet MS", s, c, true);
            
            this.tf = new TextField();
            this.tf.setTextFormat(this.tfr);
            this.SetDirectly(0);
            this.addChild(this.tf);
        }
        FlipNum.prototype = new Sprite();
        
        FlipNum.prototype.SetNum = function(n){
            if (n == this.tnum) {
                //code
                return;
            }
            this.tnum = n;
            this.tf.text = this.prefix + n + this.suffix;
            this.tf.width = this.tf.textWidth; height = this.tf.textHeight;
            Tweener.addTween(this, {cnum:n, time:0.6, transition: "easeOutSine"});
            this.addEventListener2(Event.ENTER_FRAME, this.onEF, this);
        }
        
        FlipNum.prototype.SetDirectly = function(n){
            if (n == this.tnum) {
                //code
                return;
            }
            this.tnum = this.cnum = n;
            this.tf.text = this.prefix + n + this.suffix;
            this.tf.width = this.tf.textWidth;
            height = this.tf.textHeight;
        }
        
        FlipNum.prototype.onEF = function(e){
            if (this.cnum == this.tnum) {
                //code
                this.removeEventListener(Event.ENTER_FRAME, this.onEF, this);
            }
            var ntext = this.prefix + Math.floor(this.cnum) + this.suffix;
            if (ntext == this.tf.text) {
                //code
                return;
            }
            this.tf.text = ntext;
            this.tf.width = this.tf.textWidth; height = this.tf.textHeight;
        }
        
        function View(model) {
            //code
            Sprite.call(this);
            this.model = model;
            
            this.mfish = new Fish();
            this.addChild(this.mfish);
            this.fishes = [];
            
            for(var i = 0 ; i < model.nof ;i++){
                var f = new Fish();
                this.fishes.push(f);
                this.addChild(f);
            }
        }
        
        View.prototype = new Sprite();
        
        View.prototype.Update = function(){
            var m = this.model;
            this.mfish.x = m.mfish.x;
            this.mfish.y = m.mfish.y;
            this.mfish.UpdateRadDir(m.mfish.rad, 1);
            this.mfish.update();
            
            for(var i = 0 ; i < m.fishes.length ; i++){
                var f = this.fishes[i];
                f.update();
                var mf = m.fishes[i];
                f.x = mf.x;
                f.y = mf.y;
                f.UpdateRadDir(mf.rad, mf.dir.x);
            }
        }
        
        function Model() {
            //code
            this.nof = 20;// number of fish
            this.ffr = 10;// first fish length
            this.acoeff = 4000;// map area / fish rad ^ 2 coefficient
            this.area = 0;
            this.bArea = 0;
            this.w2h = 1;
            this.maprx = 0;
            this.mapry = 0;
            this.over = false;
            this.sizeChanged = false;
            
            this.mfish = new MFish();
            this.fishes = [];
            
            for(var i = 0 ; i < this.nof ; i++)
                this.fishes.push(new MFish());
        }
        
        Model.prototype.Reset = function(){
            this.area = this.bArea = this.ffr * this.ffr * this.acoeff;
            this.updateMapSize();
            this.mfish.SetRad(this.ffr);
            this.mfish.x = this.mfish.y = 0;
            
            for(var i = 0 ; i < this.nof; i++)
                this.ResetFish(i);
        }
        
        Model.prototype.IsOver = function(){
            var over = this.over;
            this.over = false;
            return false;
        }
        
        Model.prototype.SizeChanged = function(){
            var sc = this.sizeChanged;
            this.sizeChanged = false;
            return sc;
        }
        
        Model.prototype.SetFishPos = function(x, y){
            this.mfish.x = x;
            this.mfish.y = y;
        }
        
        Model.prototype.Step = function(){
            var mx = this.mfish.x;
            var my = this.mfish.y;
            var mr = this.mfish.rad;
            var brdr = this.mfish.rad * 4;
            
            if (mr * mr * this.acoeff / this.bArea > 4) {
                //code
                this.bArea *= 4;
                this.area *= 2;
                this.updateMapSize();
                this.sizeChanged = true;
            }
            
            for(var i = 0 ; i < this.fishes.length ; i++){
                var f = this.fishes[i];
                
                var rdx = 0.14 * mr * (0.5 + 0.5 * Math.random());
                var rdy = 0.14 * mr * (0.5 + 0.5 * Math.random());
                var sgn = Math.random() > 0.5 ? 1:-1;
                if (f.x < -this.maprx - brdr) {
                    //code
                    f.SetDir(rdx, sgn * rdy);
                    f.SetRad(Model.around(mr));
                }else if (f.x > this.maprx + brdr) {
                    //code
                    f.SetDir(-rdx, sgn * rdy);
                    f.SetRad(Model.around(mr));
                }
                
                if (f.y < -this.mapry - brdr) {
                    //code
                    f.SetDir(sgn * rdx, rdy);
                    f.SetRad(Model.around(mr));
                }else if (f.y > this.mapry + brdr) {
                    //code
                    f.SetDir(sgn * rdx, - rdy);
                    f.SetRad(Model.around(mr));
                }
                
                if (Point.distance(this.mfish, f) < (mr + f.rad)) {
                    //code
                    if (f.rad > mr) {
                        //code
                        this.over = true;
                    }else{
                        this.mfish.AddToRad(f.rad * 0.08);
                        this.ResetFish(i);
                    }                    
                }
                f.x += f.dir.x;
                f.y += f.dir.y;
            }
            
        }
        
        Model.prototype.ResetFish = function(i){
            var f = this.fishes[i];
            
            f.SetRad(Model.around(this.mfish.rad));
            
            var mr = this.mfish.rad;
            var rdx = 0.14 * mr * (0.5 + 0.5 * Math.random());
            var rdy = 0.14 * mr * (0.5 + 0.5 * Math.random());
            
            if (Math.random() < 0.5) {
                //code
                f.x = -this.maprx;
            }else{
                f.x = this.maprx;
            }
            f.SetDir(rdx, rdy);
            f.y = -this.mapry + 2 * Math.random() * this.mapry;
            
        }
        
        Model.prototype.SetSizeRatio = function(w2h){
            this.w2h = w2h;
            this.updateMapSize();
        }
        Model.prototype.updateMapSize = function(){
            this.mapry = 0.5 * Math.sqrt(this.area / this.w2h);
            this.maprx = this.w2h * this.mapry;
        }
        
        Model.around = function(n){
            return (n * (0.75 + 1.2 * Math.random()));
        }
        
        function MFish() {
            //code
            this.x = 0;
            this.y = 0;
            this.rad = 0;
            this.dir = new Point(1, 1);            
        }
        
        MFish.prototype.SetRad = function(r){
            this.rad = r;
        }
        
        MFish.prototype.AddToRad = function(r){
            this.rad += r;
        }
        
        MFish.prototype.SetDir = function(dx, dy){
            this.dir.x = dx;
            this.dir.y = dy;
        }
        
        // background for game
        function Background(width, height) {
            //code
            Resizable.apply(this);
            this.w = width;
            this.h = height;
            this.bg;
            this.bubbles = [];//bong bong noi len
            this.corals = [];// reu chuyen dong ben duoi
            this.t = 0;//?
            
            this.mouseChildren = false;
            this.mouseEnabled = false;
            
            var bd = new BitmapData("images/blue_bg.jpg");
            bd.loader.addEventListener2(Event.COMPLETE, this.onBDLoaded, this);
            this.bg = new Bitmap(bd);
            
            this.addChild(this.bg);
            
            this.stuff = new Sprite();
            this.addChild(this.stuff);
            
            var bbd = new BitmapData("images/bubble.png");
            for (var i = 0 ; i < 20 ; i++) {
                var b = new Bitmap(bbd);
                this.bubbles.push(b);
                this.stuff.addChild(b);
                b.x = -width/2 + Math.random() * width;
                b.y = -height/2 + Math.random() * height;
                b.scaleX = b.scaleY =  0.14 + Math.random() * 0.4;
                b.alpha = 0.1 + 0.5 * b.scaleX;
            }
            
            var cbd = new BitmapData("images/coral.png");
            for(var i = 0 ; i < 10 ; i++){
                var b = new Bitmap(cbd);
                this.corals.push(b);
                this.stuff.addChild(b);
                b.y = 140 + 80 * (i%4);
                b.alpha = 0.4 + ((i % 2 == 0)?0:0.25);
                b.scaleX = b.scaleY = 1.4 + Math.random() * ((i%2==0)?0.7:0.4);
            }
            
            this.addEventListener2(Event.ENTER_FRAME, this.onEF, this);
        }
        
        Background.prototype = new Resizable();
        
        Background.prototype.resize = function(wi, hi){
            var rbg = wi/this.w;
            Resizable.prototype.resize.call(this, wi, hi);
            this.bg.SetWidth(wi);
            this.bg.SetHeight(hi);
            this.stuff.x = wi/2;
            this.stuff.y = hi/2;
        }
        Background.prototype.onBDLoaded = function(e){
            this.resize(this.w, this.h);
        }
        
        Background.prototype.UpdateScale = function(sc){
            Tweener.addTween(this.stuff, {scaleX:sc, scaleY:sc, transition:"easeInOutSine", time: 1});            
        }
        
        Background.prototype.onEF = function(e){
            var th = e.target;
            ++th.t;
            
            var sc = 1/this.stuff.scaleX;
            for(var i = 0 ; i < th.bubbles.length ; i++){
                var b = th.bubbles[i];
                b.y -= 10 * b.scaleX * b.scaleX;
                if (b.y < -(0.5*this.h*sc)) {
                    //code
                    b.y = 0.5*this.h*sc;
                    b.x = (-0.5*th.w + Math.random() * th.w) * sc;
                }
            }
            
            var cx;
            var coef = 0.1 * th.w * 1.05;
            for(var i = 0 ; i < th.corals.length ; i++){
                var b = th.corals[i];
                cx = -0.7 * this.w + i * coef;
                b.x = cx + Math.sin((cx * 0.15 + th.t)*0.02)*50;
            }
            
        }
        
        //----------- UTILS FOR WORK WITH COOKIES
        function createCookie(name, value, days) {
            //code
            if (days) {
                //code
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires="+ date.toGMTString();
            }
            else var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }
        
        function readCookie(name) {
	    var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	}
	
	function eraseCookie(name) {
            createCookie(name,"",-1);
    	}
        webGLStart();
    </script>
</body>
</html>
